%%%%%%%%
%%% FILTRATION
%%%%%%%%
close all; 
clear all;

%% Reads the model
[m,p,mss] = readmodel(true);

%% Set variances for Kalman filtration
p.std_e_lgdp_gap = 1;
p.std_e_dot_cpi_x = 1;
p.std_e_dot_cpi_oil = 1;
p.std_e_dot_cpi_food = 1;
p.std_e_dot_cpi = 1;
p.std_e_ls = 1;
p.std_e_rn = 1;
p.std_e_dot_s_tar = 1;
p.std_e_target = 1;
p.std_e_lx_gdp_gap = 1;
p.std_e_x_rn = 1;
p.std_e_dot_x_cpi = 1;
p.std_e_x_rr_eq  = 0.2;
p.std_e_rr_eq = 0.5;
p.std_e_dot_z_eq = 0.5;
p.std_e_prem = 1;
p.std_e_cr_prem = 1;
p.std_e_dot_gdp_eq = 1;
p.std_e_lz_food_gap = 1;
p.std_e_dot_wfood = 10;
p.std_e_dot_woil = 10;
p.std_e_dot_s_cross = 1;

m = assign(m,p);
m = solve(m);

%% Data sample
sdate = qq(1998,1);
edate = qq(2014,1);

%% Load data
d = dbload('history.csv');

dd.LCPI = d.lcpi;
dd.DOT_CPI_FOOD = d.dot_cpi_food;
dd.DOT_CPI_OIL = d.dot_cpi_oil;
dd.LGDP = d.lgdp;
dd.LS = d.ls;
dd.RN = d.rn;
dd.LX_CPI = d.lx_cpi;
dd.X_RN = d.x_rn;
dd.DOT_CPI_X = d.dot_cpi_x;
dd.DOT_WFOOD = d.dot_wfood;
dd.DOT_WOIL = d.dot_woil;
dd.DOT_S_CROSS = d.dot_s_cross;
dd.LX_GDP_GAP = d.lx_gdp_gap;
dd.TARGET = d.target;

dd.LGDP_EQ = tseries();
dd.LZ_EQ = tseries();
dd.RR_EQ = tseries();
dd.LGDP_EQ(sdate:qq(2007,4)) = d.lgdp_eq(sdate:qq(2007,4));
dd.LZ_EQ(sdate:qq(2007,4)) = d.lz_eq(sdate:qq(2007,4));
dd.RR_EQ(sdate:qq(2003,4)) = d.rr_eq(sdate:qq(2003,4));

%% Filtration
[m_kf,g,v,delta,pe] = filter(m,dd,sdate:edate);

h = g.mean;
d = dbextend(d,h);

%% Save the database
% Database is saved in file 'kalm_his.mat'
dbsave(d,'kalm_his.csv');

%% Graphs
% Specify country
country = 'The Czech Republic - Filtration';

% Report
x = report.new(country,'visible',true);

% Figures
rng = sdate:edate;
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'r'};
sty.axes.box = 'off';
sty.legend.location='Best';

x.figure('Observed and Trends','subplot',[2,3],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('GDP','legend',false);
x.series('',[d.lgdp d.lgdp_eq]);

x.graph('Real Interest Rate','legend',false);
x.series('',[d.rr d.rr_eq]);

x.graph('Foreign Real Interest Rate','legend',false);
x.series('',[d.x_rr d.x_rr_eq]);

x.graph('Real Exchange Rate','legend',false);
x.series('',[d.lz d.lz_eq]);

x.graph('Change in Eq. Real Exchange rate','legend',false);
x.series('',[d.dot_z_eq]);

x.graph('Risk Premium','legend',false);
x.series('',[d.prem]);

x.pagebreak();

x.figure('Gaps','subplot',[3,3],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('Core Inflation','legend',false);
x.series('',[d.dot_cpi_x]);

x.graph('Marginal Cost','legend',false);
x.series('',[d.rmc]);

x.graph('GDP GAP','legend',false);
x.series('',[d.lgdp_gap]);

x.graph('Monetary Conditions','legend',false);
x.series('',[d.mci]);

x.graph('Real Interest Rate Gap','legend',false);
x.series('',[d.rr_gap]);

x.graph('Real Exchange Rate Gap','legend',false);
x.series('',[d.lz_gap]);

x.graph('Food Inflation','legend',false);
x.series('',[d.dot_cpi_food]);

x.graph('Relative Food Price (Gap)','legend',false);
x.series('',[d.lz_food_gap]);

x.graph('Foreign GDP Gap','legend',false);
x.series('',[d.lx_gdp_gap]);

x.figure('Residuals','subplot',[3,3],'style',sty,'range',rng,'dateformat','YY:P');

x.graph('Core Inflation','legend',false);
x.series('',[d.e_dot_cpi_x]);

x.graph('Food Prices','legend',false);
x.series('',[d.e_dot_cpi_food]);

x.graph('Oil Prices','legend',false);
x.series('',[d.e_dot_cpi_oil]);

x.graph('GDP GAP','legend',false);
x.series('',[d.e_lgdp_gap]);

x.graph('Interest Rate','legend',false);
x.series('',[d.e_rn]);

x.graph('Exchange Rate','legend',false);
x.series('',[d.e_ls]);

x.graph('Relative Food Price (Gap)','legend',false);
x.series('',[d.e_lz_food_gap]);

x.graph('Trend Real Interest Rate','legend',false);
x.series('',[d.e_rr_eq]);

x.graph('Trend Real Exchange Rate','legend',false);
x.series('',[d.e_dot_z_eq]);

%adding a new line to include mk_report.m file
mk_report(d,dd, sdate:edate);

x.publish('Filtration','display',false);
disp('Done!!!');